package com.zhitong.money.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Description:
 * date: 2021/1/31 10:35
 *
 * @author Andy Zhang
 * @since JDK 1.8
 */
@Mapper
public interface RolePerMapper {

    @Insert("insert into role_permission values(#{rid},#{pid})")
    public void add(String rid, String pid);

    @Select("select pid from role_permission where rid = #{rid}")
    public List<String> findPidsByRid(String rid);

    @Delete("delete from role_permission where rid = #{rid}")
    public void deleteByRid(String rid);

    @Delete("delete from role_permission where pid = #{pid}")
    public void deleteByPid(String pid);

    @Delete({
            "<script>",
            "delete from role_permission",
            "where rid in",
            "<foreach item='item' collection='rids' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public int deleteByRids(List<String> rids);

    @Delete({
            "<script>",
            "delete from role_permission",
            "where pid in",
            "<foreach item='item' collection='pids' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public int deleteByPids(List<String> pids);

    @Select("select count(*) from role_permission where pid = #{pid}")
    public int countByPid(String pid);

    @Select({
            "<script>",
            "select count(*) from role_permission",
            "where pid in",
            "<foreach item='item' collection='pids' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public int countByPids(List<String> pids);
}
